#ifndef __GAME_CURL_H__
#define __GAME_CURL_H__

#include "game_def.h"
#include <curl/curl.h>
#include <vector>
#include <functional>

class CGameCurl{
public:
  typedef std::vector<BYTE> RecvBuffer_t;
  typedef size_t write_callback_t(BYTE *data, size_t size);
  typedef curl_socket_t opensock_callback_t(curlsocktype purpose, struct curl_sockaddr *address);
  typedef int sockopt_callback_t(curl_socket_t curlfd, curlsocktype purpose);
private:
  time_t _start_time;
  int _timeout;
  std::function<write_callback_t> _writeCallback;
  std::function<opensock_callback_t> _opensockCallback;
  std::function<sockopt_callback_t> _sockoptCallback;
public:
  BOOL init();
  CURLcode get(const char* url,FILE* fp);
  CURLcode get(const char* url,RecvBuffer_t* buff);
  CURLcode get(const char* url,RecvBuffer_t* buff,int timeout);

  // 允许用户处理curl细节的一系列API
  CURLcode get(const char* url);
  void setWriteCallback(std::function<write_callback_t> callback);
  void setOpenSockCallback(std::function<opensock_callback_t> callback);
  void setSockOptCallback(std::function<sockopt_callback_t> callback);
  static size_t static_write_callback(void *ptr, size_t size, size_t nmemb, void *stream);
  static curl_socket_t static_opensock_callback(void *clientp, curlsocktype purpose, struct curl_sockaddr *address);
  static int static_sockopt_callback(void *clientp, curl_socket_t curlfd, curlsocktype purpose);
};

extern CGameCurl g_CGameCurl;

#endif
